<%
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", -1); //prevents caching at the proxy server
%>
<% int validaSession = 3; %>
<%@ include file="Connections/session.jsp" %>

<%!
Driver DriverRs2=null ; //= (Driver)Class.forName(MM_conDB_DRIVER).newInstance();
Connection ConnRs2=null ; // = DriverManager.getConnection(MM_conDB_STRING,MM_conDB_USERNAME,MM_conDB_PASSWORD);
Object data2;
String query2 ="";
PreparedStatement StatementRs2=null;
boolean existeconexion=false;
void abreconexion(String MM_conDB_DRIVER2,String MM_conDB_STRING2,String MM_conDB_USERNAME2,String MM_conDB_PASSWORD2){
	try{
		DriverRs2 = (Driver)Class.forName(MM_conDB_DRIVER2).newInstance();
		ConnRs2 = DriverManager.getConnection(MM_conDB_STRING2,MM_conDB_USERNAME2,MM_conDB_PASSWORD2);
		existeconexion=true;
	}catch(Exception sqle){
//		out.print(sqle);
	}
}

int countRow = 1;
//Funcion Rama
String rama(String idalmacen,String idmotivolote,String idorigen, int secuencia){
	String retornar="";
	if(existeconexion){
		try{
			retornar+="<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
			query2="SELECT DISTINCT a.id_almacen,a.nombre"; //
			query2+=" FROM VISTA_UNIDAD vu,ALMACEN a WHERE a.id_almacen=vu.almacen_destino";
			query2+=" AND vu.id_lote = "+idmotivolote+" AND vu.almacen_origen = "+idalmacen+" AND  (vu.status = 'Transito' OR vu.status = 'Recepcionado')";
			query2+=" ORDER BY a.nombre";
//			out.println(query2);
			StatementRs2 = ConnRs2.prepareStatement(query2);
			ResultSet queryRs2 = StatementRs2.executeQuery();
			int i =0;
			
			while(queryRs2.next()){
				if(i > 0)
					countRow++;
				i++;
				String id_almacen = queryRs2.getString("id_almacen");
		      	retornar+="  <tr>"; //            liquidacion_maiz2.jsp?id_almac="+id_almacen+"&IdMotivoLote="+idmotivolote+"&banderaML=0
				retornar+="  <td class=\"textcolumna\" width=\"10\" align=\"center\" valign=\"top\"><font size=\"2\">&raquo;</font></td>";
				retornar+="  <td class=\"textcolumna\" valign=\"top\"><a href=\"javascript:abrirliquidacion2("+id_almacen+","+idmotivolote+")\">"+(((data2=queryRs2.getObject("nombre"))==null)?"":data2)+"</a></td>";
				if(!idorigen.equals(id_almacen)){
		      		retornar+="  <td class=\"textcolumna\" valign=\"top\">"+rama(id_almacen, idmotivolote, idorigen, secuencia)+"</td>";
						
				}else{
					retornar+="  <td class=\"textcolumna\" valign=\"top\">&nbsp;</td>";
				}
				retornar+="  </tr>";
			}queryRs2.close();
			retornar+="</table>";
		}catch(Exception e){
//			out.print(e);
		}
	}else{
	}
	return retornar;
} // Eo String rama



void cierraconexion(){
	try{
		ConnRs2.close();
	}catch(Exception e){
	}
}	//	Eo void cierra conexion

%>
<%@page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*,java.text.*"%>
<%@ include file="Connections/conDB.jsp" %>
<%
try {
Driver DriverRs = (Driver)Class.forName(MM_conDB_DRIVER).newInstance();
Connection ConnRs = DriverManager.getConnection(MM_conDB_STRING,MM_conDB_USERNAME,MM_conDB_PASSWORD);
abreconexion(MM_conDB_DRIVER,MM_conDB_STRING,MM_conDB_USERNAME,MM_conDB_PASSWORD);
String query ="";
Object data=null;
PreparedStatement StatementRs =null;
ResultSet queryRs=null;

boolean reportar=false;
String id_almac=request.getParameter("id_almac");
id_almac = id_almac!=null && !id_almac.equals("")?id_almac:"-1";
String IdMotivoLote=request.getParameter("IdMotivoLote");
IdMotivoLote = IdMotivoLote!=null && !IdMotivoLote.equals("")?IdMotivoLote:"-1";

String Hora=request.getParameter("Hora");
if(Hora==null || Hora.length()<=0){Hora="";}
String Fecha=request.getParameter("Fecha");
Fecha= Fecha!=null && !Fecha.equals("")?Fecha:"";

String query3 = "SELECT u.almacen_destino, cu.contrato, c.lote," +
 	"a.nombre as bodega FROM unidad u, contrato_unidad cu," +
	" contrato c, almacen a where u.id_unidad = cu.unidad AND" +
	"a.id_almacen = u.almacen_destino AND  cu.contrato=" + IdMotivoLote +" AND c.id_contrato = " + IdMotivoLote+ " GROUP BY "+
	"u.almacen_destino, cu.contrato, c.lote, a.nombre;";

int banderaDatos = 0;
//if(IdMotivoLote.length()>0 && Hora.length()>0 && Fecha.length()>0){
	reportar=true;
//}
%>
<html>
<head>
<title>CIMA v2 - Liquidaci&oacute;n de Compra de Ma&iacute;z</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="miestilo.css" type="text/css">
</head>
<script>
var win1 = null
//-------
function enviaSubmit() {
	//document.form1.imprimir.value = "false";
//	alert("enviando info");
	document.form1.submit();
}
//-------
function abrircatalogo2() {
	win1 = window.open('motivolote_catalogo.jsp?campo1=Fecha&campo2=IdMotivoLote&campo3=Hora','Patron','top=220,left=65,width=350,height=270');
}
//-------
function cierraVentana() {
	obj1 = eval("win1");
	if (obj1 && !obj1.closed) {
		obj1.close();
	}			
}
//----------
function abrirliquidacion2(id_almacen,IdMotivoLote){
	winx = window.open('liquidacion_maiz2.jsp?id_almac='+id_almacen+'&IdMotivoLote='+IdMotivoLote+'&banderaML=0','Reportes_L2','scrollbars=yes,top=200,left=200,width=600,height=450');
}
//----------
function reporteOrDes(id_almacen, id_lote, motLote) {
	window.open('liquidacion_maizOrDes.jsp?id_almac='+id_almacen+'&IdMotivoLote='+id_lote+'&Fecha='+motLote,'RepLiqOrDes','scrollbars=yes,top=120,left=80,width=750,height=420');
}
//----------
function reporteOrDes2(id_almacen, id_lote, motLote) {
	window.open('liquidacion_NuevamaizOrDes.jsp?id_almac='+id_almacen+'&IdMotivoLote='+id_lote+'&Fecha='+motLote,'RepLiqOrDes','scrollbars=yes,top=120,left=80,width=750,height=420');
}
</script>
<body bgcolor="#FFFFFF" text="#000000" background="images/fondo.jpg" leftmargin="0" topmargin="5">
<form name="form1" method="post" action="liquidacion_maiz.jsp">
<center>
<% out.print(reportar);%>
<br>
<%
if(reportar){
%>
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td class="titcolumna" colspan="2">&nbsp;Origen</td>
    </tr>
    <%
    String str_nombre = "";
    query="SELECT a.nombre,a.id_almacen FROM LOTE l, ALMACEN a WHERE l.almacen=a.id_almacen AND l.id_lote = "+IdMotivoLote+" AND a.id_almacen = "+id_almac+"";
    StatementRs=ConnRs.prepareStatement(query);
    queryRs=StatementRs.executeQuery();
    if(queryRs.next()){
    	String id_almacen=queryRs.getString("id_almacen");
		banderaDatos = 1;
		%>
		<tr>
		  <td>
		<table border="0" cellpadding="0" cellspacing="0">
			<tr>
			<td class="textcolumna" valign="top"><a href="javascript:abrirliquidacion2(<%=id_almacen%>,<%=IdMotivoLote%>)"><%=(((data=queryRs.getObject("nombre"))==null)?"":data)%></a></td>
			  <td class="textcolumna" valign="top"><%=rama(id_almacen,IdMotivoLote,id_almacen,1)%></td>
		  </tr>
		</table>
		<td>
		</tr>
		<%
	}
	else {
		query="SELECT a.nombre,a.id_almacen FROM LOTE l, ALMACEN a WHERE isnull (l.almacen, "+id_almac+") = a.id_almacen  and l.id_lote = "+IdMotivoLote+" AND a.id_almacen = "+id_almac+" and l.compra is null and l.almacen is null";
		queryRs.close();
		StatementRs=ConnRs.prepareStatement(query);
		queryRs=StatementRs.executeQuery();
		if(queryRs.next()){
			String id_almacen=queryRs.getString("id_almacen");
			banderaDatos = 1;
		%>
		<tr>
		  <td>
		<table border="0" cellpadding="0" cellspacing="0">
			<tr>
			<td class="textcolumna" valign="top"><a href="javascript:abrirliquidacion2(<%=id_almacen%>,<%=IdMotivoLote%>)"><%=(((data=queryRs.getObject("nombre"))==null)?"":data)%></a></td>
			  <td class="textcolumna" valign="top"><%=rama(id_almacen,IdMotivoLote,id_almacen, 1)%></td>
		  </tr>
		</table>
		<td>
		</tr>
    <%
		}queryRs.close();
	}
    %>
</table><br>
<%
}
%>
<input type="hidden" name="id_almac" value="<%=id_almac%>">
<input type="hidden" name="IdMotivoLote" value="<%=IdMotivoLote%>">
<input type="hidden" name="Hora" value="<%=Hora%>">
    <table align="center" cellpadding="0" cellspacing="0" width="300">
	<% if (banderaDatos == 1) { %>
      <tr>
	    <td align="center"><a href="javascript:reporteOrDes('<%=id_almac%>','<%=IdMotivoLote%>','<%=Fecha%>')">&raquo; Liquidaci&oacute;n de Compra de Ma&iacute;z Origen-Destino final</a></td>
	</tr>
      <tr>
	    <td align="center"><a href="javascript:reporteOrDes2('<%=id_almac%>','<%=IdMotivoLote%>','<%=Fecha%>')">&raquo; Nueva Liquidaci&oacute;n de Compra de Ma&iacute;z </a></td>
	</tr>
	<tr>
	  <td>&nbsp;</td>
	</tr>
	<% } %>
   <tr> 
        <td align="center"><a href="javascript:window.close()"><img src="images/bCerrar.gif" width="82" height="18" border="0"></a></td>
  </tr>
</table>
</center>
</form>
</body>
<HEAD>
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
</HEAD>
</html>
<%
ConnRs.close();
cierraconexion();
}
catch (Exception e) {
	out.println(e.getMessage());
}
%>
<script>
<%
if(countRow>1) {
	int int_restopx = countRow*15;
	out.println( "if( 150+"+int_restopx+" <= screen.availHeight ){" );
	out.println( "     window.moveTo( ( (screen.width - 600) / 2 ),( (screen.height - ( 250+"+int_restopx+")) / 2 ) );" );
	out.println( "     window.resizeTo(600 ,250+"+int_restopx+");" );
	out.println( "}else{");
	out.println( "     window.moveTo( ( (screen.width - 600) / 2 ),( (screen.height - ( screen.availHeight )) / 2 ) );" );
	out.println( "     window.resizeTo(600 ,screen.availHeight);" );
	out.println( "}");
	} 
%>
</script>

